﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using SelfService.Model;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using WalkingTec.Mvvm.Core;


namespace SelfService.Tasks
{
    [Display(Name = "更新医疗项目")]
    public class UpdateMedicalProjectsTask : BaseTask
    {
        private readonly IServiceProvider _provider;
        public UpdateMedicalProjectsTask(WTMContext context, IServiceProvider provider) : base(context)
        {
            _provider = provider;
        }

        public override async Task<bool> Process()
        {
            this.Logger("开始同步医疗项目", ActionLogTypesEnum.Debug);
            try
            {
                //调用HIS接口
                IHisOrder service;
                try
                {
                    var scope = _provider.CreateScope();
                    service = scope.ServiceProvider.GetRequiredService<IHisOrder>();
                    //service = Context.ServiceProvider.GetRequiredService<IHisOrder>();
                }
                catch (Exception)
                {
                    this.Logger($"没有启用HisOrder服务", ActionLogTypesEnum.Exception);
                    return false;
                }
                if (service == null)
                {
                    this.Logger($"没有启用HisOrder服务", ActionLogTypesEnum.Exception);
                    return false;
                }
                var data = await service.GetProjectList();
                if (data.Code == 200)
                {
                    var dc = Context.DC;
                    foreach (var element in data.Data)
                    {
                        var project = await dc.Set<SysMedicalProject>()
                            .FirstOrDefaultAsync(e => e.Code == element.Code);
                        if (project == null)
                        {
                            project = element;
                            project.CreateTime = DateTime.Now;
                            project.UpdateTime = DateTime.Now;
                            project.ID = Guid.NewGuid();
                            await dc.Set<SysMedicalProject>().AddAsync(project);
                        }
                        else
                        {
                            project.Name = element.Name;
                            project.Price = element.Price;
                            project.UpdateTime = DateTime.Now;
                        }
                    }
                    await dc.SaveChangesAsync();
                    this.Logger($"同步医疗项目完成, 本地同步数据:{data.Data.Length}条", ActionLogTypesEnum.Debug);
                    return true;
                }
                else
                {
                    this.Logger($"查询缴费清单, 请求数据:{data.Request}, 返回数据:{data.Response}", ActionLogTypesEnum.Exception);
                }
            }
            catch (Exception e)
            {
                this.Logger($"同步医疗项目发生异常{e.Message}, 详细信息:{JsonConvert.SerializeObject(e)}", ActionLogTypesEnum.Exception);
            }
            return false;
        }
    }
}
